﻿using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.Spreadsheet.Model;

namespace SmartPlan.TelerikExt
{
    public static class SpreadsheetAttachedProperties
    {
        public static readonly DependencyProperty TemplateWorkbookProperty =
            DependencyProperty.RegisterAttached("TemplateWorkbook", typeof(Workbook),
            typeof(SpreadsheetAttachedProperties), new PropertyMetadata(null, OnTemplateWorkbookChanged));

        public static Workbook GetTemplateWorkbook(DependencyObject obj)
        {
            return (Workbook)obj.GetValue(TemplateWorkbookProperty);
        }

        public static void SetTemplateWorkbook(DependencyObject obj, Workbook value)
        {
            obj.SetValue(TemplateWorkbookProperty, value);
        }

        private static void OnTemplateWorkbookChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var spreadsheet = d as RadSpreadsheet;
            if (spreadsheet != null)
            {
                spreadsheet.Workbook = e.NewValue != null ? (Workbook)e.NewValue : new Workbook();
            }
        }
    }
}
